﻿using System.Activities;

namespace WFT.Activities
{
    /// <summary>
    /// Create a bookmark
    /// </summary>
    public class CreateBookmarkActivity : CreateBookmarkActivity<object>
    {

    }

    /// <summary>
    /// Create a bookmark with a return value
    /// </summary>
    /// <typeparam name="T">Type of the return value</typeparam>
    public class CreateBookmarkActivity<T> : NativeActivity<T>
    {
        /// <summary>
        /// Bookmark name
        /// <remarks>
        /// Required Argument
        /// </remarks>
        /// </summary>
        [RequiredArgument]
        public InArgument<string> BookmarkName { get; set; }

        /// <summary>
        /// Bookmark options
        /// </summary>
        public BookmarkOptions BookmarkOptions { get; set; }

        public CreateBookmarkActivity()
        {
            BookmarkOptions = System.Activities.BookmarkOptions.None;
        }


        protected override bool CanInduceIdle
        {
            get
            {
                return true;
            }
        }

        protected override void Execute(NativeActivityContext context)
        {
            string bookmarkName = BookmarkName.Get(context);

            context.CreateBookmark(bookmarkName, new BookmarkCallback(OnBookmarkCallback));
        }

        void OnBookmarkCallback(NativeActivityContext context, Bookmark bookmark, object state)
        {
            Result.Set(context, (T)state);
        }
    }
}
